package com.android.lovely.ui.activity

import android.os.Bundle
import android.transition.TransitionManager
import android.view.ViewGroup.MarginLayoutParams
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import com.android.lovely.api.service.systemService
import com.android.lovely.api.service.userService
import com.android.lovely.data.remoteSigned
import com.android.lovely.databinding.ActivitySplashBinding
import com.android.lovely.ext.fullScreen
import com.android.lovely.framework.BaseMvvmActivity
import com.android.lovely.framework.doContinuousOperation
import com.android.lovely.manager.KVStore
import com.android.lovely.manager.WorkStore
import com.android.lovely.ui.dialog.UserDialog
import com.android.lovely.ui.viewmodel.LoginViewModel
import com.android.lovely.user.UserManager
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport.Session.User
import com.kotlin.extensions.context.ActivityExt.navBarHeight
import com.kotlin.extensions.context.ContextExt.startPage
import com.kotlin.extensions.coroutine.CoroutineExt.doOnIO
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import com.kotlin.extensions.dimen.Dimension.dp
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.withTimeoutOrNull

/**
 *
 * @author why
 * @date 2025/3/7 18:27
 */
class SplashActivity : BaseMvvmActivity<ActivitySplashBinding, LoginViewModel>() {

    override fun initView(savedInstanceState: Bundle?) {
        if (!isTaskRoot) {
            finish()
            return
        }
        fullScreen()
        mBinding.login.update(viewModel)
        mBinding.normal.llBottom.updateLayoutParams<MarginLayoutParams> {
            bottomMargin = navBarHeight + 35.dp
        }
        if (intent.getBooleanExtra("logout", false)) {
            showLoginLayout()
        } else {
            initWork()
        }
    }

    override suspend fun initData() {
        viewModel.userLogin.collectLatest {
            next()
        }
    }

    private fun showLoginLayout() {
        // 开始过渡动画
        TransitionManager.beginDelayedTransition(mBinding.root)
        // 隐藏 normal 布局
        mBinding.normalContent.visibility = android.view.View.GONE
        // 显示 login 布局
        mBinding.login.visibility = android.view.View.VISIBLE
    }

    private fun initWork() {
        lifecycleScope.launchSafety {
            val minDeffer = async { delay(1500) }
            val configDeffer = async {
                if (UserManager.isLogin()) {
                    UserManager.refreshAuth()
                    withTimeoutOrNull(10000) {
                        doContinuousOperation {
                            KVStore.system = systemService.getSystemConfig()
                        }
                    }
                }
            }
            val signDeffer = async {
                if (UserManager.isLogin()) {
                    withTimeoutOrNull(10000) {
                        doContinuousOperation {
                            remoteSigned =  doOnIO {
                                userService.getSignList().checked
                            }
                        }
                    }
                }
            }
            signDeffer.await()
            minDeffer.await()
            configDeffer.await()
            next()
        }
    }

    private fun next() {
        if (UserManager.isLogin().not()) {
            showLoginLayout()
            return
        }
        if (UserManager.userInfo?.name.isNullOrBlank()) {
            UserDialog().show(supportFragmentManager)
            return
        }
        startPage<HomeActivity>()
        finish()
    }
}